#include  "ATD.h"
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
} LNode, *LinkList;

Status InitList(LinkList &L)
{
    L = (LinkList)malloc(sizeof(LNode));
    if (!L)exit(OVERFLOW);
    L->next = NULL;
    printf("Initialed.\n");
    return OK;
}

Status DestroyList(LinkList &L)
{
    LinkList tmpNext;
    while (L)
    {
        tmpNext = L->next;
        free(L);
        L = tmpNext;
    }
    printf("Destroyed.\n");
    return OK;
}

Status ClearList(LinkList &L)
{
    if (L->next)
    {
        return FALSE;
    }
    LinkList tmpNode = L->next, tmpNext;
    while (tmpNode->next)
    {
        tmpNext = tmpNode->next;
        free(tmpNode);
        tmpNode = tmpNext;
    }
    printf("Clear.\n");
    return OK;
}

Status ListEmpty(LinkList L)
{
    if (!L->next)
        return TRUE;
    else
        return FALSE;
}

Status ListLength(LinkList L)
{
    int count = 0;
    LinkList currentElem = L;
    while (currentElem->next)
    {
        currentElem = currentElem->next;
        count++;
    }
    return count;
}

Status GetElem(LinkList L, int i, ElemType &e)
{
    if (i <= 1 && i >= ListLength(L)) exit(0);
    LinkList tmpNode = L;
    for (int count = 0; count < i && tmpNode->next; ++count)
    {
        tmpNode = tmpNode->next;
    }
    e = tmpNode->data;
    return OK;
}

Status LocateElem(LinkList L, ElemType e, int (*compare)(ElemType p, ElemType q))
{
    int i = 0; LinkList tmpNode = L;
    while (!compare(tmpNode->data, e) && tmpNode->next)
    {
        tmpNode = tmpNode->next;
        ++i;
    }
    if (i == 0 || tmpNode == NULL)
    {
        printf("No such node.\n");
        return FALSE;
    }
    else
    {
        return i;
    }
}


Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e)
{
    for (LinkList tmpNode = L, preNode = NULL; tmpNode->next; tmpNode = tmpNode->next)
    {
        if (tmpNode->data == cur_e)
        {
            pre_e = preNode->data;
            return OK;
        }
        else
        {
            preNode = tmpNode;
        }
    }
    return FALSE;
}

Status NextElem(LinkList L, ElemType cur_e, ElemType &next_e)
{
    for (LinkList tmpNode = L; tmpNode->next; tmpNode = tmpNode->next)
    {
        if (tmpNode->data == cur_e)
        {
            next_e = tmpNode->next->data;
            return OK;
        }
    }
    return FALSE;
}

Status ListInsert(LinkList &L, int i, ElemType e)
{
    if (i < 0 || i > (ListLength(L) + 1)) exit(FALSE);
    LinkList tmpNode = L;
    for (int count = 0; count < i && tmpNode->next; ++count)
        tmpNode = tmpNode->next;
    LinkList newNode = (LinkList)malloc(sizeof(LNode));
    if (!newNode) exit(OVERFLOW);
    newNode->data = e;
    newNode->next = tmpNode->next;
    tmpNode->next = newNode;
    return OK;
}

Status ListDelete(LinkList &L, int i, ElemType &e)
{
    if (i < 0 || i > (ListLength(L) + 1)) exit(FALSE);
    LinkList tmpNode = L, preNode = NULL;
    for (int count = 0; count < i && tmpNode->next; ++count)
    {
        preNode = tmpNode;
        tmpNode = tmpNode->next;
    }
    preNode->next = tmpNode->next;
    free(tmpNode);
    return OK;
}
//int main()
//{
//    LinkList L;
//    InitList(L);
//
//
//    if (ListEmpty(L)) printf("ListEmpty.\n");
//    for (int i = 1; i < 5; ++i)
//    {
//        ListInsert(L, i, i);
//    }
//    ElemType e;
//    GetElem(L, 3, e);
//    printf("The 3rd = %d\n", e);
//    printf("Len = %d\n", ListLength(L));
//    ClearList(L);
//
//    DestroyList(L);
//    system("pause");
//    return 0;
//}